<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/7
 * Time: 下午3:24
 */
class Login extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('url');
        $this->load->helper('common');
        $this->load->model('user_model');
        $this->load->model('customer_model');
        $this->load->model('admin_model');
        $this->load->library('session');
    }

    // 测试登录
    public function test_login()
    {
        $customer_id = 8;
        $this->session->set_userdata('customer_id', $customer_id);
        $sql = 'select username,shop_user_id,company,name,identity,status,activation from app_customer WHERE id = ' . $customer_id;
        $customer_info = $this->db->query($sql)->row_array();
        $this->session->set_userdata('customer_info', $customer_info);
    }

    //判断是否登录
    public function index()
    {
        if ($this->session->customer_id) {
            if ($_SERVER['HTTP_REFERER']) {
                redirect($_SERVER['HTTP_REFERER'], 'refresh');
            } else {
                redirect(base_url(), 'refresh');
            }
        } else {

        }
    }

    function check()
    {
        if ($this->session->customer_id) {
            echo json_encode(array('status' => 1));
        } else {
            echo json_encode(array('status' => 0));
        }
        exit;
    }

    public function login()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_customer_id'])) {
            $shop_customer_id = $post['shop_customer_id'];
            // 商城返回过来的shop_customer_id 查询具体详情
            $city_manager_base_url = $this->config->item("city_manager_base_url");
            $api_url = $city_manager_base_url . '/customer/get_customer_info_api';
            $res = curl_request($api_url, ['shop_customer_id' => $shop_customer_id]);
            if ($res) {
                $res_arr = json_decode($res, true);
                if ($res_arr['status'] == 'success') {
                    // 保存客户信息
                    $res = $this->admin_model->save_admin_info_by_shop_customer_id($shop_customer_id, $res_arr['data']);
                    if ($res) {
                        $this->session->set_userdata('customer_id', $res);
                        $sql = 'select username,shop_user_id,company,name,identity,status,activation from app_customer WHERE id = ' . $res;
                        $customer_info = $this->db->query($sql)->row_array();
                        $this->session->set_userdata('customer_info', $customer_info);
                        send_json(array('code' => 0, 'data' => ['activation' => $customer_info['activation']], 'msg' => "登录成功"));
                    } else {
                        send_json(array('code' => -1, 'data' => '', 'msg' => "登录失败"));
                    }

                } else {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "登录失败"));
                }
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "登录失败，参数错误"));
        }

    }


    // 登出
    public function logout()
    {
        if ($this->session->customer_id) {
            $this->session->unset_userdata(['customer_id', 'customer_info']);
            //send_json(array('code'=>0,'data'=>'','msg'=>"注销成功"));
        } else {
            // send_json(array('code'=>0,'data'=>'','msg'=>"尚未登录"));
        }
        redirect('/', 'refresh');
    }

    // 登出
    public function logout_ajax()
    {
        if ($this->session->customer_id) {
            $this->session->unset_userdata(['customer_id', 'customer_info']);
        } else {
            //send_json(array('code'=>0,'data'=>'','msg'=>"尚未登录"));
        }
        send_json(array('code' => 0, 'data' => '', 'msg' => "注销成功"));
    }



    // 模拟登录
    public function login_auto()
    {
        $customer_id = $_GET['id'];
        if ($customer_id == null) {
            exit('id is null');
        }
        $this->session->set_userdata('customer_id', $customer_id);
        $sql = 'select username,shop_user_id,company,name,identity,status from app_customer WHERE id = ' . $customer_id;
        $customer_info = $this->db->query($sql)->row_array();
        $this->session->set_userdata('customer_info', $customer_info);
        ob_end_clean();
        header("Content-type:text/html;charset=utf-8");
        echo "登录成功";
    }

    // 模拟登出
    public function logout_auto()
    {
        if ($this->session->customer_id) {
            $this->session->unset_userdata(array('customer_id', 'customer_info'));
        } else {
        }
        ob_end_clean();
        header("Content-type:text/html;charset=utf-8");
        echo "退出成功";
    }
}